package utils

import (
	"encoding/json"
	"log/slog"
	"reflect"
)

type MsgError struct {
	Code       int    `json:"code,omitempty"`
	HttpStatus int    `json:"httpStatus,omitempty"`
	Message    string `json:"message,omitempty"`
}

func (t MsgError) Error() string {
	data, err := json.Marshal(t)
	if err != nil {
		slog.Error("failed to json msgError", "err", err)
		return err.Error()
	}

	return string(data)
}

func NewErr(httpStatus int, code int, msg string) *MsgError {
	return &MsgError{
		HttpStatus: httpStatus,
		Code:       code,
		Message:    msg,
	}
}

func IsNil(i interface{}) bool {
	if i == nil {
		return true
	}
	value := reflect.ValueOf(i)
	return value.Kind() == reflect.Pointer && value.IsNil()
}
